package com.kbbywyclient.presenter;

import android.content.Context;

import com.kbbywyclient.R;
import com.kbbywyclient.data.model.base.BaseModel;
import com.kbbywyclient.presenter.base.BasePresenter;
import com.kbbywyclient.service.ApiService;
import com.kbbywyclient.service.ApiServiceHelper;
import com.kbbywyclient.ui.contarct.CommissionWithdrawContract;
import com.kbbywyclient.util.HttpExcUtil;
import com.kbbywyclient.util.RsaJsonUtil;
import com.kbbywyclient.util.ToastUtil;
import com.kbbywyclient.util.customservice.ApiException;

import javax.inject.Inject;

import rx.Subscriber;

/**
 * Created by Jiangzx on 2017/9/16 10:14.
 */

public class CommissionWithdrawPresenter extends BasePresenter implements CommissionWithdrawContract.Presenter {
    @Inject
    Context mContext;
    @Inject
    ApiService apiService;
    @Inject
    RsaJsonUtil jsonUtil;

    private CommissionWithdrawContract.View view;

    @Inject
    public CommissionWithdrawPresenter(CommissionWithdrawContract.View view){
        this.view=view;
    }
    @Override
    public void withdraw(int salesId,final double amount) {
        view.showLoadingDialog(mContext.getString(R.string.loading));
        jsonUtil.put("salesID",salesId);
        jsonUtil.put("amount",String.valueOf(amount));
        ApiServiceHelper.
                appointThreadNormal(apiService.withdraw(jsonUtil.getRsaParams())).
                subscribe(new Subscriber<BaseModel>() {
                    @Override
                    public void onCompleted() {

                    }

                    @Override
                    public void onError(Throwable e) {
                        view.closeLoadingDialog();
                        if(e instanceof ApiException){
                            ToastUtil.showShort("提现失败");
                        }else{
                            HttpExcUtil.printError(e);
                        }
                    }

                    @Override
                    public void onNext(BaseModel o) {
                        view.closeLoadingDialog();
                        view.onSubmitSuccess(amount);
                    }
                });
    }
}
